// pages/thinkTank.js
var app = getApp();
var util = require('../../utils/util.js');
var inputContent = {};//输入内容
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: ['重庆', '成都', '西安', '贵阳'],
    realEstate: ['有抵押', '无抵押'],
    credit: ['有不良征信', '无不良征信'],
    items: [
      { id: '1', value: '我同意《贷投狗用户服务协议及隐私声明》', checked: 'true' },
    ],
    data: {
      customerName: "", // 称呼
      cityName: "", // 城市
      property: "", //房产
      creditStatus: "", //征信
      phoneNumber: "", //手机
      code: "", //验证码
      demandId: "" //推广人id
    },
    paracont: "获取验证码",
    vcdisabled: true,//验证码按钮状态
    checkbox: 1,
    addtell: {
      addtellHidden: true, //弹出框显示/隐藏
  },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const scene = decodeURIComponent(options.scene)
    this.setData({
      'data.demandId':scene
  })
  },
  footAddtell: function () {
    //打开弹出框
    this.setData({
        addtell: {
            addtellHidden: false,
            contract_info: ''
        }
    })
},
modalConfirm: function () {
    //弹出框确认操作
    this.setData({
        addtell: {
            addtellHidden: true,
        }
    })
},
modalCancel: function () {
    //弹出框取消操作
    this.setData({
        addtell: {
            addtellHidden: true,
        }
    })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  checkboxChange(e) {
    let checkbox = e.detail.value
    console.log(checkbox)
    this.setData({
      checkbox:checkbox
    })
  },
  // 称呼
  customerName: function (e) {
    console.log(e)
    let that = this
    that.setData({
      'data.customerName': e.detail.value
    })
  },
  // 城市
  clickCity(e) {
    console.log(e)
    let that = this
    that.setData({
      'data.cityName': e.currentTarget.dataset.name
    })
  },
  // 房产
  property: function (e) {
    let that = this
    that.setData({
      'data.property': e.currentTarget.dataset.name
    })
  },
  // 征信
  creditStatus: function (e) {
    let that = this
    let credit = e.currentTarget.dataset.name
    let list = ""
    if (credit == '有不良征信') {
      list = 1
    } else if (credit == '无不良征信') {
      list = 2
    }
    that.setData({
      'data.creditStatus': list,
      creditstyle: credit
    })
    console.log(that.data.creditStatus)
  },
  // 手机
  phoneNumber: function (e) {
    let that = this
    that.setData({
      'data.phoneNumber': e.detail.value
    })
    inputContent[e.currentTarget.id] = e.detail.value;
    util.verifyCodeBtn(e, this);
    console.log(that.data.phoneNumber)
  },
  // 验证码
  code: function (e) {
    let that = this
    that.setData({
      'data.code': e.detail.value
    })
  },
  /**
 * 获取验证码
 */
  getVerifyCode: function (e) {
    var that = this;
    util.getVerifyCode(inputContent['user'], this, function (data) {
      that.setData({
        verifycode: data.data
      })
    })

  },
  // 提交
  subim: function () {
    let that = this
    let data = this.data.data
    if (that.data.checkbox != 1 ) {
      wx.showModal({
        title: '提示',
        content: '请选择我同意',
        showCancel: false,
        confirmText: '确定',
      })
      return false
    }
    if(data.customerName == "" || data.cityName == "" || data.property == "" || data.creditStatus == "" || data.phoneNumber == "" || data.code == "") {
      wx.showModal({
        title: '提示',
        content: '不能允许有空值',
        showCancel: false,
        confirmText: '确定',
      })
      return false
    }
    
      wx.request({
        url: app.globalData.api + '/app/auto/saveDemand', //上线的话必须是https，没有appId的本地请求貌似不受影响 
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT 
        header: {
            'Content-Type': "application/json",
        }, // 设置请求的 header
        data,
        success: function (res) {
            console.log(res.data)
            if (res.data.code == 200) {
              wx.showModal({
                title: '提示',
                content: res.data.message,
                showCancel: false,
                confirmText: '确定',
                success: function () {
                  that.setData({
                    'data.customerName' : "",
                    'data.cityName' : "",
                    'data.property' : "",
                    'data.creditStatus' : "",
                    creditstyle : "",
                    'data.phoneNumber' : "",
                    'data.code' : ""
                  })
                }
            })
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.message,
                showCancel: false,
                confirmText: '确定',
              })
            }
        },
        fail: function () {
        },
        complete: function () {
            // complete 
        }
    })
  }
})